//
//  YBstorydetialTableViewCell.m
//  OurTourism
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "YBstorydetialTableViewCell.h"
#import "Header.h"
@interface YBstorydetialTableViewCell ()

@property (nonatomic, retain)UIImageView *img;
@property (nonatomic, retain)UILabel *label;
@end


@implementation YBstorydetialTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
}
- (void)createView
{
    self.img = [[UIImageView alloc]init];
    [self.contentView addSubview:self.img];

    self.label = [[UILabel alloc]init];
    self.label.font = [UIFont systemFontOfSize:15];
    self.label.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.label];
    
 
}



- (void)layoutSubviews
{
    
    [super layoutSubviews];

    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(20);
        make.left.mas_equalTo(self.contentView).offset(20);
        make.size.mas_equalTo(CGSizeMake(WIDTH - 40,  (self.detailModel.photo_height).integerValue  * WIDTH / (self.detailModel.photo_width).integerValue));
    }];
    
    CGFloat hLabel = [[self class]heightWithText:self.label.text fontSize:15 Width:WIDTH - 40];

    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.img).offset(20+  (self.detailModel.photo_height).integerValue  * WIDTH / (self.detailModel.photo_width).integerValue);
        make.left.mas_equalTo(self.img);
        make.size.mas_equalTo(CGSizeMake(WIDTH - 40, hLabel));
        
    }];
 
    self.label.numberOfLines = 0;
}

- (void)setDetailModel:(YBDestinationModel *)detailModel
{
    if (_detailModel != detailModel) {
        _detailModel = detailModel;
    }
    
    [self.img sd_setImageWithURL:[NSURL URLWithString:detailModel.photo]] ;
    self.label.text = detailModel.text;
    
}


//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
